package challengesManager;

import java.awt.event.ActionListener;
import java.io.ObjectInputStream.GetField;

import javax.swing.JPanel;

import control.Control;

import data.ChallengeData;
import data.Player;

/**
 * In this class all logic concerning one single challenge should be stored.
 * @author Yuliy
 *
 */
public abstract class ChallengeManager{
	
	/**
	 * Defines the current state of the challenge (see state machine model).
	 * <br> The challenge is currently being played.
	 */
	public static final int STATE_RUNNING = 1;
	
	/**
	 * Defines the current state of the challenge (see state machine model).
	 * <br> The challenge is currently paused but not finished. The manager expects input from outside to resume action.
	 */
	public static final int STATE_PAUSED = 2;
	
	/**
	 * Defines the current state of the challenge (see state machine model).
	 * <br> The challenge is currently being loaded. It prepares for execution and should be not interrupted.
	 */
	public static final int STATE_LOADING = 3;
	
	/**
	 * Defines the current state of the challenge (see state machine model).
	 * <br> The challenge has finished loading and awaits the call of start() to start the game.
	 */
	public static final int STATE_LOADED_AND_READY = 4;
	
	/**
	 * Defines the current state of the challenge (see state machine model).
	 * <br> The challenge has been played and is now over. It can be replaced.
	 */
	public static final int STATE_FINISHED = 5;
	
	/**
	 * Defines the current state of the challenge (see state machine model).
	 * <br> An error occurred during execution.
	 */
	public static final int STATE_ERROR = -1;
	
	/**
	 * Defines the current state of the challenge (see state machine model).
	 * <br> The state of the challenge is unknown or could not be defined.
	 */
	public static final int STATE_UNDEFINED = 0;
	
	private int state = STATE_UNDEFINED;
	protected Control c;
	protected ChallengeData d;
	
	/**
	 * Constructor with field assignments.
	 * In this method the GUI should be initialized and all preparations for the start of the challenge should be made.
	 * 
	 * <br>Don't forget to add the newly created ChallengeGUI to the place holder panel!
	 * 
	 * 
	 */
	
	public ChallengeManager(Control c, ChallengeData d) {
		this.c = c;
		this.d = d;
	}
	
	public int getState() {
		return state;
	}

	/**
	 * This method starts the challenge and the players will now play until all rounds of this challenge are played.
	 */
	public abstract void start();
	
	/**
	 * This method will be called from the KeyInputManager when one of the players has pressed his or her buzzer button.
	 * @param pl the player who has pressed his buzzer button
	 */
	public abstract void playerBuzz(Player pl);
	
	/**
	 * This method will be called from ChallengeGUI with parameter true when the event occurs which the players are waiting for.
	 * It should be called at the earliest moment for a player to buzz and win the current round of the challenge.
	 * Usually used for logging a time stamp.
	 */
	public abstract void perfectBuzzMoment(boolean state);
	
	/**
	 * This will be called from ChallengeGUI when the animation prepared for one round of this challenge is over.
	 * Now the manager can decide what to do next. It could show the players that they have failed to buzz,
	 * reset variables, return control to the Control class if this challenge is over or start another round with ChallengeGUI.newRound().
	 */
	public abstract void animationOver();

	public abstract void disposeGui();
	/**
	 * @param placeHolderPanel This is a reference to the panel from the global user interface to which the ChallengeGUI panel should be added.
	 */
	public abstract void initGui(JPanel placeHolderPanel);
	
	protected void setState(int state){
		this.state = state;
	}
	
	public ChallengeData getData(){
		return d;
	}

	public abstract void disableBuzzing();

	public abstract void enableBuzzing();

}
